package com.best.onegous.details;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.best.onegous.BaseActivity;
import com.best.onegous.R;
import com.best.onegous.Adapter.UserAdapter;
import com.best.onegous.Refresh.XListView;
import com.best.onegous.Refresh.XListView.IXListViewListener;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class User extends BaseActivity implements OnClickListener,OnScrollListener,IXListViewListener {
	private XListView listView;
	private List<JSONObject> list = new ArrayList<JSONObject>(); 
	private UserAdapter adapter;
	private View headerView,headerView1;
	private Intent intent;
	private TextView ones_top,ones_but,twos_top,twos_but,threes_top,threes_but,one_top,one_but,two_top,two_but,three_top,three_but;
	private LinearLayout ones,twos,threes,titles,one,two,three;
	private LayoutInflater inflater;
	private int headerHeight;
	private RelativeLayout rl_title;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setThem(R.layout.activity_user);
		/*intent = getIntent();
		id = intent.getStringExtra("id");*/
		inflater = LayoutInflater.from(this);
		rl_title = (RelativeLayout) findViewById(R.id.rl_title);
		rl_title.getBackground().setAlpha(0);
		findViewById(R.id.back).setOnClickListener(this);
		listView = (XListView) findViewById(R.id.listview);
		titles = (LinearLayout) findViewById(R.id.titles);
		
		
		ones = (LinearLayout) findViewById(R.id.ones);
		ones.setOnClickListener(new ToolsItemListener(0));
		twos = (LinearLayout) findViewById(R.id.twos);
		twos.setOnClickListener(new ToolsItemListener(1));
		threes = (LinearLayout) findViewById(R.id.threes);
		threes.setOnClickListener(new ToolsItemListener(2));

		ones_top = (TextView) findViewById(R.id.ones_top);
		ones_but = (TextView) findViewById(R.id.ones_but);
		twos_top = (TextView) findViewById(R.id.twos_top);
		twos_but = (TextView) findViewById(R.id.twos_but);
		threes_top = (TextView) findViewById(R.id.threes_top);
		threes_but = (TextView) findViewById(R.id.threes_but);

		
		headerView = inflater.inflate(R.layout.header_user, null);
		listView.addHeaderView(headerView);
		
		
		
		headerView1 = inflater.inflate(R.layout.header_title_top, null);
		one = (LinearLayout) headerView1.findViewById(R.id.one);
		one.setOnClickListener(new ToolsItemListener(0));
		two = (LinearLayout) headerView1.findViewById(R.id.two);
		two.setOnClickListener(new ToolsItemListener(1));
		three = (LinearLayout) headerView1.findViewById(R.id.three);
		three.setOnClickListener(new ToolsItemListener(2));

		one_top = (TextView) headerView1.findViewById(R.id.one_top);
		one_but = (TextView) headerView1.findViewById(R.id.one_but);
		two_top = (TextView) headerView1.findViewById(R.id.two_top);
		two_but = (TextView) headerView1.findViewById(R.id.two_but);
		three_top = (TextView) headerView1.findViewById(R.id.three_top);
		three_but = (TextView) headerView1.findViewById(R.id.three_but);
		listView.addHeaderView(headerView1);
		
		
        for(int i =0;i<60;i++){
        	list.add(new JSONObject());
        }
        adapter = new UserAdapter(this,list);
       
        listView.setAdapter(adapter);
       // listView.refresh();
        listView.setPullLoadEnable(true);
        listView.setPullRefreshEnable(false);
     // ���ù�������
     		listView.setOnScrollListener(this);
        listView.setXListViewListener(this);
        

	}
	private class ToolsItemListener implements View.OnClickListener{
		private int id;
		public ToolsItemListener(int id){
			this.id=id;
		}
		
		@Override
		public void onClick(View v) {
			clearTitle();
			switch (id) {
			case 0:
			
				ones_top.setTextColor(getResources().getColor(R.color.them));
				ones_but.setTextColor(getResources().getColor(R.color.them));
				
				one_top.setTextColor(getResources().getColor(R.color.them));
				one_but.setTextColor(getResources().getColor(R.color.them));
				adapter.setItem(0);
				break;
			case 1:
				twos_top.setTextColor(getResources().getColor(R.color.them));
				twos_but.setTextColor(getResources().getColor(R.color.them));
				
				two_top.setTextColor(getResources().getColor(R.color.them));
				two_but.setTextColor(getResources().getColor(R.color.them));
				adapter.setItem(1);
				break;
			case 2:
				threes_top.setTextColor(getResources().getColor(R.color.them));
				threes_but.setTextColor(getResources().getColor(R.color.them));
				
				three_top.setTextColor(getResources().getColor(R.color.them));
				three_but.setTextColor(getResources().getColor(R.color.them));
				adapter.setItem(2);
				break;
			
			default:
				
				break;
			}
		}
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			 finish();
			
			break;
		
		}
	}

	private void clearTitle() {
		// TODO Auto-generated method stub
		ones_top.setTextColor(getResources().getColor(R.color.zi));
		ones_but.setTextColor(getResources().getColor(R.color.zi));
		twos_top.setTextColor(getResources().getColor(R.color.zi));
		twos_but.setTextColor(getResources().getColor(R.color.zi));
		threes_top.setTextColor(getResources().getColor(R.color.zi));
		threes_but.setTextColor(getResources().getColor(R.color.zi));
		one_top.setTextColor(getResources().getColor(R.color.zi));
		one_but.setTextColor(getResources().getColor(R.color.zi));
		two_top.setTextColor(getResources().getColor(R.color.zi));
		two_but.setTextColor(getResources().getColor(R.color.zi));
		three_top.setTextColor(getResources().getColor(R.color.zi));
		three_but.setTextColor(getResources().getColor(R.color.zi));
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		/*if (firstVisibleItem >= 2) {
			titles.setVisibility(View.VISIBLE);
		} else {
			titles.setVisibility(View.GONE);
		}*/
		if (firstVisibleItem == 1) {
			//toast("1");
			// ��ȡͷ����
			View views = listView.getChildAt(0);
			if (views != null) {
				// ��ȡͷ�������ڵ����ϲ���λ�õ��෴��
				int top = -views.getTop();
				// ��ȡͷ���ֵĸ߶�
				headerHeight = views.getHeight();
				//Log.i("ssssssssssssssss", top+"-"+headerHeight+"-"+dip2px(this, 45));
				// �������������ʱ����ͷ������XListview���������һ���ؼ���ʱ��ֻ�����ʱ�����ǲŵ���͸����
				if (top <= headerHeight && top >= 0) {
					
					// ��ȡ��ǰλ��ռͷ���ָ߶ȵİٷֱ�
					float f = (float) top / (float) headerHeight;
					int s = (int) (f * 255);
					rl_title.getBackground().setAlpha(s);
					// ֪ͨ������ˢ����ʾ
					rl_title.invalidate();
					
					
					titles.setVisibility(View.GONE);
				}
				if(top+dip2px(this, 45)>=headerHeight){
					titles.setVisibility(View.VISIBLE);
					rl_title.getBackground().setAlpha(255);
				}
			}
		} else if (firstVisibleItem > 1) {
			rl_title.getBackground().setAlpha(255);
			titles.setVisibility(View.VISIBLE);
			
		} else {
			titles.setVisibility(View.GONE);
			rl_title.getBackground().setAlpha(0);
		}
	}
	  /** 
     * dip��dpת����px �������?ͬ�ֱ�·����Ļ 
     * @param context 
     * @param dpValue 
     * @return 
     */  
    private int dip2px(Context context,float dpValue){  
        float scale = context.getResources().getDisplayMetrics().density;  
        return (int)(dpValue*scale +0.5f);  
    }  
	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				listView.stopLoadMore();
			}
		}, 2000);
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				listView.stopRefresh();
			}
		}, 2000);
	}
	

}
